package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.TFlupStrategyDto;
import com.ruoyi.system.domain.po.TFlupStrategy;
import com.ruoyi.system.domain.po.TFlupStrategyExample;
import com.ruoyi.system.domain.vo.TFlupStrategyVo;
import com.ruoyi.system.mapper.TFlupStrategyMapper;
import com.ruoyi.system.service.TFlupStrategyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TFlupStrategyServiceImpl implements TFlupStrategyService {
    
    @Resource
    private TFlupStrategyMapper flupStrategyMapper;
    
    
    @Override
    public Integer insertFlupStrategy(TFlupStrategyDto.Insert flupStrategyInsert) {
        // 新增实体数据
        TFlupStrategy flupStrategy = BeanUtil.copyProperties(flupStrategyInsert, TFlupStrategy.class);
        flupStrategy.setId(IdUtil.getSnowflake().nextId());
        flupStrategy.setCreateBy(SecurityUtils.getUsername());
        flupStrategy.setCreateTime(new Date());
        return flupStrategyMapper.insertSelective(flupStrategy);
    }

    @Override
    public Integer updateFlupStrategy(TFlupStrategyDto.Update flupStrategyUpdate) {
        // 修改实体
        TFlupStrategy flupStrategy = BeanUtil.copyProperties(flupStrategyUpdate, TFlupStrategy.class);
        flupStrategy.setUpdateBy(SecurityUtils.getUsername());
        flupStrategy.setUpdateTime(new Date());
        return flupStrategyMapper.updateByPrimaryKeySelective(flupStrategy);
    }

    @Override
    public Integer deleteFlupStrategy(TFlupStrategyDto.Delete flupStrategyDelete) {
        TFlupStrategyExample interfaceExample = new TFlupStrategyExample();
        interfaceExample.createCriteria().andIdIn(flupStrategyDelete.getIdList());
        TFlupStrategy flupStrategy = new TFlupStrategy();
        flupStrategy.setDelFlag((byte) 2);
        return flupStrategyMapper.updateByExampleSelective(flupStrategy, interfaceExample);
    }

    @Override
    public List<TFlupStrategyVo.Result> listFlupStrategy(TFlupStrategyDto.Query queryFlupStrategy) {
        TFlupStrategyExample flupStrategyExample = new TFlupStrategyExample();
        TFlupStrategyExample.Criteria criteria = flupStrategyExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (queryFlupStrategy.getType() != null) {
            criteria.andTypeEqualTo(queryFlupStrategy.getType());
        }
        if (queryFlupStrategy.getRiskLevel() != null) {
            criteria.andRiskLevelEqualTo(queryFlupStrategy.getRiskLevel());
        }
        flupStrategyExample.setOrderByClause("create_time desc");
        List<TFlupStrategy> flupStrategyList = flupStrategyMapper.selectByExample(flupStrategyExample);
        return flupStrategyList.stream().map(e -> BeanUtil.copyProperties(e, TFlupStrategyVo.Result.class)).collect(Collectors.toList());
    }
}
